package com.pospi.xstore.ui.static_ui;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.pospi.xstore.R;
import com.pospi.xstore.adapter.RefundAdapter;
import com.pospi.xstore.dao.OrderDao;
import com.pospi.xstore.dto.OrderDto;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.util.GetData;

import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderFindActivity extends BaseActivity {

    @Bind(R.id.et_order)
    EditText etOrder;
    @Bind(R.id.lv_order)
    ListView lvOrder;
    @Bind(R.id.sale_ranking_data)
    TextView saleRankingData;
    @Bind(R.id.sale_ranking_data_to)
    TextView saleRankingDataTo;
    @Bind(R.id.search)
    Button search;
    private int fromData;
    private int toData;

    private List<OrderDto> orderDtos;
    private RefundAdapter adapter;
    public static final String FindOrder = "findOrder";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_order_find);
        setTitle("订单查询");
        ButterKnife.bind(this);

        lvOrder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(OrderFindActivity.this, FindOrderDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(FindOrder, orderDtos.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        saleRankingData.setText(GetData.getYYMMDDTime());
        fromData = GetData.getSearchTime();

        saleRankingDataTo.setText(GetData.getYYMMDDTime());
        toData = GetData.getSearchTime();

        adapteData();
    }

    public void adapteData() {
//        orderDtos = new OrderDao(getApplicationContext()).selectGoodsBetween(fromData, toData);
//        adapter = new RefundAdapter(getApplicationContext(), orderDtos);
//        lvOrder.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        etOrder.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隐藏键盘
                    boolean sd = ((InputMethodManager) etOrder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(
                                    OrderFindActivity.this.getCurrentFocus().getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    searchOrder();
                    return true;
                }
                return false;
            }
        });
    }

    public void searchOrder() {
        String maxNo;
        try {
            maxNo = etOrder.getText().toString().trim();
            OrderDto selectDto = new OrderDao(getApplicationContext()).selectOrderByNo(maxNo);
            if (selectDto == null) {
                showToast("未查询到该订单");
            } else {
                orderDtos.clear();
                orderDtos.add(selectDto);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            showToast("请输入合适的订单号");
        }
    }

    @OnClick({R.id.sale_ranking_data, R.id.sale_ranking_data_to, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sale_ranking_data:
                showPickerDialog(saleRankingData, SaleRankingActivity.FROM);
                break;
            case R.id.sale_ranking_data_to:
                showPickerDialog(saleRankingDataTo, SaleRankingActivity.TO);
                break;
            case R.id.search:
                if (toData >= fromData & toData <= GetData.getSearchTime()) {
                    adapteData();
                } else {
                    showToast("请选择正确的查询日期");
                }
                break;
        }
    }

    public void showPickerDialog(final TextView tv, final int num) {
        final Calendar c1 = Calendar.getInstance();
        DatePickerDialog dialog1 = new DatePickerDialog(OrderFindActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                c1.set(year, monthOfYear, dayOfMonth);
                tv.setText(DateFormat.format("yyyy-MM-dd", c1));
                switch (num) {
                    case SaleRankingActivity.FROM:
                        fromData = Integer.parseInt(DateFormat.format("yyyyMMdd", c1).toString());
                        break;
                    case SaleRankingActivity.TO:
                        toData = Integer.parseInt(DateFormat.format("yyyyMMdd", c1).toString());
                        break;
                }
            }
        }, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH));
        dialog1.show();
    }
}